package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.diandian.backend.entrance.client.DianDianSmsClient;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.SmsSendDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterSmsService {

    private static final Logger logger = LoggerFactory.getLogger(RouterSmsService.class);

    @Autowired
    private DianDianSmsClient diandianSmsClient;

    /**
     * 子路由服务
     * @param clientKey
     * @param clientSecret
     * @param data
     * {
     *     "cmd":"sms.send"
     * }
     * @return
     */
    public Object execute(String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 发送短信
        if(EntranceConstant.ROUTER_SMS_SEND.equals(cmd)){

            logger.info("执行短信发送请求，参数: {}", data.toJSONString());

            // 参数转换
            SmsSendDTO smsSendDTO = JSONObject.parseObject(data.toString(), SmsSendDTO.class);

            // 执行短信发送
            BaseResult resultDTO = diandianSmsClient.sendSmsInner(smsSendDTO);

            logger.info("短信发送请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
